<template>
	<view>
		<view v-if="data != '' " class="pb100">
			<view class="tops bgw p30">
				<view v-show="state == 0" @tap="setTab()" class="fr">
					管理
				</view>
				<view v-show="state == 1" @tap="successTab()" class="fr">
					完成
				</view>
			</view>
			<view class="contents">
				<view v-for="(v,i) in data" :key="i" class="main p30">
					<view class="time"> {{v.date}} </view>
					<view v-for="(j,k) in v.data" :key="k" class="lists">
						<view @tap="select(i,k,j)" :class="{selectAct:state ==1}" class="box bgw">
							<view v-show="state == 1">
								<image v-show="j.act == false" class="select" src="../../static/image/nomal@2x.png"></image>
								<image v-show="j.act == true" class="select" src="../../static/image/select@2x.png"></image>
							</view>
							<view class="shop">
								<image :src="j.product.thumbnail" mode="aspectFit"></image>
								<view v-if="j.product.isMarketable == false" class="noneGoods">
									已下架
								</view>
							</view>
							<view class="txt">
								<p class="p1 text-df two">
									{{j.product.name}}
								</p>
								<p class="p3 ">
									<view> ¥<span> {{j.product.price}} </span></view>
								</p>
							</view>
						</view>
					</view>
				</view>
				<view v-show="state == 1" class="bottoms">
					<view @tap="all()" class="fl">
						<image v-show="allState == 0" class="vm" src="../../static/image/nomal@2x.png" mode=""></image>
						<image v-show="allState == 1" class="vm" src="../../static/image/select@2x.png" mode=""></image>
						全选
					</view>
					<view class="fr">
						<p @tap="delect()">删除</p>
					</view>
				</view>
			</view>
		</view>
		<view v-if="noneData ==1 " class="dataNone">
			<image src="../../static/image/guanzhu@2x.png" mode=""></image>
			<p>暂无浏览记录</p>
			<u-button @tap="Goindex()" :ripple="true" shape="circle" type="success">
				去逛逛
			</u-button>
		</view>
	</view>
</template>

<script>
	import {
		historyList,
		historyRemove
	} from '../../api/api.js'
	export default {
		data() {
			return {
				state: 0,
				pageNumber: 1,
				pageSize: 100,
				stops: 0,
				allState: 0,
				data: [],
				noneData:0
			};
		},
		created() {
			this.init()
		},
		onReachBottom() {
			if (this.stops == 0) {
				this.init()
			}
		},
		methods: {
			Goindex(){
				uni.switchTab({
					url:'../index/index'
				})
			},
			async delect() {
				let datas = this.data
				let ids = []
				for (let i = 0; i < datas.length; i++) {
					let record = datas[i].data
					for (let k = 0; k < record.length; k++) {
						if (record[k].act == true) {
							ids.push(record[k].id)
						}

					}
				}
				let data = await historyRemove({
					ids: ids
				})
				this.stops = 0
				this.data = []
				this.pageNumber = 1
				this.init()
			},
			all() {
				let datas = this.data
				if (this.allState == 0) {
					this.allState = 1
					for (let i = 0; i < datas.length; i++) {
						let record = datas[i].data
						for (let k = 0; k < record.length; k++) {
							record[k].act = true
						}
					}
				} else {
					this.allState = 0
					for (let i = 0; i < datas.length; i++) {
						let record = datas[i].data
						for (let k = 0; k < record.length; k++) {
							record[k].act = false
						}
					}
				}


			},
			select(i, k, j) { 
				if (this.state == 0) {
					if (j.product.isMarketable == true) {
						this.$navigateTo('../GoodsDetails/GoodsDetails?id=' + this.data[i].data[k].product.id)
					}else{
						this.$u.toast(`该商品已下架`);
					}
				} else {
					this.data[i].data[k].act = !this.data[i].data[k].act
				}
			},
			successTab() {
				this.state = 0
			},
			setTab() {
				this.state = 1
			},
			async init() {
				let parameret = {
					pageable: {
						pageNumber: this.pageNumber,
						pageSize: this.pageSize
					}
				}
				let datas = await historyList(parameret)
				datas = datas.data
				let arr1 = [];
				let sum = 0
				for (let i = 0; i < datas.length; i++) {
					let record = datas[i].data
					for (let k = 0; k < record.length; k++) {
						sum = sum + 1
						record[k].act = false
					}
				}
				if(this.pageNumber == 1 && datas == ''){
					this.noneData =1
				}
				this.data = [...this.data, ...datas]
				if (sum != this.pageSize) {
					this.stops = 1
				}
				this.pageNumber = this.pageNumber + 1;
			}
		}
	}
</script>

<style lang="less" scoped>
	@import './index.less';
</style>
